package cn.wizzer.framework.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;

public class Id {

    private static Integer number = 1;
    private static int maxNum = 999999;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");//年月日格式

    /**
     * 生成订单编号 17+ 位数
     *
     * @return
     */
    public static String createOrderNo() {
        number++;//唯一数字自增
        if (number >= maxNum) { // 值的上限，超过就归零
            number = 0;
        }
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        return sdf.format(new Date()) + number;//返回时间+一毫秒内唯一数字的编号，区分机器可以加字母ABC...
    }

    /**
     * 生成ID 8 位数
     *
     * @return
     */
    public static String createCode() {
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String format = sdf.format(new Date());
        return format.substring(3, 4) + format.substring(5, 6) + format.substring(7, 8) + format.substring(9, 10)
                + format.substring(11, 12) + format.substring(14, 17);
    }

    /**
     * 生成6位邮箱验证码
     */
    public static String createVerifyCode() {
        Random ran = new Random();
        Integer[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            code.append(ints[ran.nextInt(ints.length)]);
        }
        return code.toString();
    }


    public static void main(String[] args) {
        System.out.println(Id.createOrderNo());
    }

}
